package com.ymk.cmd;

import com.alibaba.fastjson.JSONObject;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.service.WxService;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.work.*;
import com.ymk.core.code.cmd.WorkCmdCode;
import com.ymk.core.code.resp.RespCode;
import com.ymk.core.constants.WorkConstants;
import com.ymk.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class WorkPostCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        JSONObject json = JSONObject.parseObject(sign.getSource());
        WorkBO reqData = super.getData(json.getString(WorkConstants.WORK_INFO), WorkBO.class);
        this.setCreatedData(respSign,reqData);
        this.workService.post(reqData);
        // 保存岗位标签信息
        List<Integer> idList = reqData.getWorkTypeIdList();

        for(int i : idList){
            ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                            .addParam("id",i);
           WorkTypeBO workTypeBO =  workTypeService.get(paramsBuilder.build());
           if (Objects.nonNull(workTypeBO)){
               WorkToTypeBO workToTypeBO = new WorkToTypeBO();
               workToTypeBO.setWorkId(reqData.getId());
               workToTypeBO.setWorkTypeId(i);
               workToTypeService.post(workToTypeBO);
           } else {
               throw new MallException(RespCode.CODE_ERROR, i+"标签不存在");
           }
        }
        // 保存岗位详情图片
        List<WorkImgBO> workImgBOList =  reqData.getWorkImgList();
        workImgBOList.forEach(workImgBO -> {
            try {
                //获取文件的http链接 可以使抖音端显示图片（抖音不识别微信云托管的cloud协议）
                String fileUrl = wxService.getDownLoadUrl(workImgBO.getImgUrl());
                workImgBO.setImgUrl(fileUrl);
                workImgBO.setWorkId(reqData.getId());
                this.setCreatedData(sign,workImgBO);
                workImgService.post(workImgBO);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        });

        //保存岗位其他信息
        saveWorkContent(reqData,json,sign);


        return respSign;
    }

    private void saveWorkContent(WorkBO workBO, JSONObject json, CmdSign sign) {
        //添加默认数据
        WorkContentBO workContent = new WorkContentBO();
        workContent.setWorkId(workBO.getId());
        this.setCreatedData(sign,workContent);
        workContentService.post(workContent);

        // 岗位补充条件
        for (Map.Entry<String, Object> entry :json.entrySet()){
            if(!entry.getKey().equals(WorkConstants.WORK_INFO)){
                WorkContentBO workContentBO = new WorkContentBO();
                workContentBO.setWorkId(workBO.getId());
                workContentBO.setContent(entry.getKey());
                workContentBO.setContentInfo(entry.getValue().toString());
                this.setCreatedData(sign,workContentBO);
                workContentService.post(workContentBO);

            }
        }



    }

    @Override
    public String getCmdCode() {
        return WorkCmdCode.WORK_POST;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {



        return null;
    }


    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }
    @Autowired
    private IWorkService workService;
    @Autowired
    private IWorkTypeService workTypeService;
    @Autowired
    private IWorkToTypeService workToTypeService;

    @Autowired
    private WxService wxService;
    @Autowired
    private IWorkContentService workContentService;
    @Autowired
    private IWorkImgService workImgService;



}